package com.claa.springboot.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LoginController {
	
	@PostMapping(value="/user/login")
	public String login(@RequestParam(value="username") String username, 
						@RequestParam(value="password") String password,
						Map<String, Object> map,
						HttpSession session) {
		if (!StringUtils.isEmpty(username) && "123".equals(password)) {
			// 登陆成功 通过跳转，防止重复提交表单
			session.setAttribute("LoginUser", username);	// 登陆用户更新到session中
			return "redirect:/main.html";
		}
		else {
			// 登陆失败
			map.put("error", "用户名或密码错误");
			return "login";
		}
	}
}
